package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional  //添加事务
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    /**
     *  查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findListByQuery(ChannelDTO dto) {
        //1.校验业务参数
            //检查分页参数
        dto.checkParam();
        //2.执行分页条件
        //2.1分页条件对象
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        // 判断前端是否传name  如果有值  添加模糊查询条件
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //  判断前端是否传status 如果有值  添加eq条件
        if (dto.getStatus()!=null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //  添加排序条件
        queryWrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //3.封装通用分页返回结果
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1. 参数校验
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        if (name.length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        // 查询满足条件的数量        (根据当前的频道名称去数据库中查询数量)
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        // 如果数量大于零 则说明数据库中已经存在这个频道名称了
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
        }
        //2.保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3.返回封装结果
        return ResponseResult.okResult();
    }

    /**
     *  修改
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        // 1.参数校验
        if (adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id为空");
        }
        AdChannel oldChannel = this.getById(adChannel.getId());
        String newname = adChannel.getName();
        if (StringUtils.isNotBlank(newname) && !newname.equals(oldChannel.getName())){
            if (newname.length() > 10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newname));
            // 如果数量大于零 则说明数据库中已经存在这个频道名称了
            if (count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
            }
        }
        // 2.根据id修改数据
        this.updateById(adChannel);
        //3.返回结果
        return ResponseResult.okResult();
    }

    /**
     *  根据id删除数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验（id 检查对应的数据 状态是否为true）
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        // 判断频道是否存在
        AdChannel adChannel = this.getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"对应的频道信息不存在");
        }
        //判断频道是否有效
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效，不允许删除");
        }
        //2.删除频道
        this.removeById(id);

//        if (id < 100){  //如果id小于100  认为是业务错误
////            throw new RuntimeException("id不能小于100");
//        throw  new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
//        CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
//        }

        //3.返回结果
        return ResponseResult.okResult();
    }
}
